﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Domain.Outboundorder;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.OutboundOrder
{
    /// <summary>
    /// 获取销售信息Handler
    /// </summary>
    public class SearchSalesHandler : IRequestHandler<SearchSalesCommand, Result>
    {
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 出库类型表
        /// </summary>
        public IBaseRepository<Sales> salesrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchSalesHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="salesrepository"></param>
        /// <param name="logger"></param>
        public SearchSalesHandler(IMapper mapper, IBaseRepository<Sales> salesrepository, ILogger<SearchSalesHandler> logger)
        {
            this.mapper = mapper;
            this.salesrepository = salesrepository;
            this.logger = logger;
        }
        /// <summary>
        /// 获取销售信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(SearchSalesCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                //获取销售信息
                var list = await(await salesrepository.GetAll()).ToListAsync();
                result.Data = list;
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "获取销售信息查询成功";
            }
            catch (Exception ex)
            {
                result.Msg = "获取销售信息查询异常" + ex.Message;
                logger.LogError("获取销售信息查询异常" + ex.Message);
            }
            return result;
        }
    }
}
